package leetcode;

import java.util.Stack;

/**
 * Created by Ghost on 2014/12/2 0002.
 */
public class Valid_Parentheses {
    private boolean cooperate(char a,char b){
        return (a=='('&&b==')')||(a=='{'&&b=='}')||(a=='['&&b==']');
    }
    public boolean isValid(String s) {
        if(s==null)
            return false;
        char[] chars = s.toCharArray();
        Stack<Character> stack = new Stack<Character>();
        for(char c:chars){
            if(c=='(' || c=='{' || c=='['){
                stack.add(c);
            }
            else{
                if(stack.isEmpty() || !cooperate(stack.peek(),c))
                    return false;
                else
                    stack.pop();
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args){
        Valid_Parentheses valid_parentheses=  new Valid_Parentheses();
        System.out.println(valid_parentheses.isValid("([]{()})"));
    }
}
